const pomelo = require('pomelo');
let logger = require('pomelo-logger').getLogger('err-log', __filename);
let userDao = {};
userDao.queryUserByUnid = async function(unid){
    return new Promise((resolve,reject)=>{
        let sql = 'select u.account,r.uid,r.img,r.sex,r.coin,r.gems,r.name,u.unid,r.diamond,r.gameId from user u left join role r on u.unid = r.userid where u.unid = ? ';
        let args = [ unid ];
        pomelo.app.get('dbclient').query(sql,args,(err,res)=>{
            if(err){
                logger.error('queryUserByUnid for userdao failed!' + err.stack);
                reject(err.stack);
            };
            console.log('res===>',res);
            if(res && res instanceof Array && res.length > 0){
                resolve(res[0]);
            }else
            {
                reject(null);
            };
        });
    });
}
userDao.queryUserByUid = async function(uid){
    return new Promise((resolve,reject)=>{
        let sql = 'select u.account,r.img,r.sex,r.coin,r.gems,r.name,u.unid,r.diamond,r.gameId from user u left join role r on u.unid = r.userid where r.uid = ? ';
        let args = [ uid ];
        pomelo.app.get('dbclient').query(sql,args,(err,res)=>{
            if(err){
                logger.error('queryUserByUnid for userdao failed!' + err.stack);
                reject(err.stack);
            };
            console.log('res===>',res);
            if(res && res instanceof Array && res.length > 0){
                resolve(res[0]);
            }else
            {
                reject(null); 
            };
        });
    });
}
module.exports = userDao;
